<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      :title="t('routes.demo.documentManager.instanceDetailSet.workflowInstanceInfo')"
      @register="register"
      @ok="handleSubmit"
      :destroyOnClose="true"
      :minHeight="450"
      :width="1200"
    >
      <BasicForm @register="registerForm" />
      <Divider style="width: 100vw" />
      <Row>
        <Col :span="6" :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
          <BasicTree
            :treeData="fileList"
            :replaceFields="{ key: 'oid', title: 'filename' }"
            @select="setSelectedKeys"
          />
        </Col>
        <Col :span="18" :xs="18" :sm="18" :md="18" :lg="18" :xl="18">
          <BasicTable @register="registerTable" :data-source="flowExampleList" />
        </Col>
      </Row>
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { BasicTree } from '/@/components/Tree/index';
  import { BasicTable, useTable } from '/@/components/Table';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Row, Col, Divider } from 'ant-design-vue';

  import { getBorrowForm, getFlowDetailColumns } from './tableData';
  import { getBorrowFlowInstance } from '/@/api/demo/document';

  const { t } = useI18n();
  const fileList = ref<any>([]);
  const oid = ref<string>('');
  const version = ref<number>(null);
  const flowExampleList = ref<any>([]);

  // 提交
  async function handleSubmit() {
    closeModal();
  }

  // 选择关联对象
  const setSelectedKeys = (keys) => {
    oid.value = keys[0];
    reload();
  };

  // 初始化弹窗
  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({ confirmLoading: false });
    const params = {
      oId: data.id,
      version: data.version,
    };
    oid.value = data.id;
    version.value = data.version;
    getBorrowFlowInstance(params).then((res) => {
      setFieldsValue(res);
      fileList.value = res.filelist;
    });
  });

  // 初始化详情表单
  const [registerForm, { setFieldsValue }] = useForm({
    schemas: getBorrowForm(),
    labelWidth: 110,
    colon: true,
    labelCol: { span: 6 },
    wrapperCol: { span: 14 },
    showResetButton: false,
    showSubmitButton: false,
    showAdvancedButton: true,
  });

  // 初始化表格
  const [registerTable, { reload }] = useTable({
    columns: getFlowDetailColumns(),
    api: getBorrowFlowInstance,
    beforeFetch: (params) => {
      params.oId = oid.value;
      params.version = version.value;
    },
    fetchSetting: {
      // 当前页传递给后台的字段名称
      pageField: 'CurrentPage',
      // 后台每页显示的数量字段名称
      sizeField: 'PageSizes',
      // 接口返回的表单数据字段名称
      listField: 'steplist',
      // 接口返回的表格总数字段名称
      totalField: 'count',
    },
    showIndexColumn: false,
    showTableSetting: false, // 表格设置工具配置
    bordered: true,
    canResize: false,
  });
</script>

<style lang="less" scoped>
  ::v-deep(.ant-calendar-picker) {
    width: 100% !important;
  }
</style>
